package com.cloudm.technician.ui.modules.cloudm_box.map_preview

import android.os.Bundle
import com.amap.api.maps.model.BitmapDescriptor
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.MarkerOptions
import com.cloudm.technician.R
import com.cloudm.technician.ui.base.BaseActivity
import kotlinx.android.synthetic.main.activity_map_preview.*
import java.util.*

/**
 * 作者: wesley
 * 时间: 2017/12/27
 * 描述:
 */
class MapPreviewActivity : BaseActivity() {

    private var lat: Double = 0.0
    private var lng: Double = 0.0
    private var location: String = ""

    override val layoutResID: Int
        get() = R.layout.activity_map_preview

    override fun handleBundle(bundle: Bundle) {
        lat = bundle.getDouble("lat")
        lng = bundle.getDouble("lng")
        location = bundle.getString("location")
    }

    override fun initVariables() {

    }

    override fun initViews(savedInstanceState: Bundle?) {
        headerTitle = R.string.location
        displayHomeAsUpEnabled = true

        mapView.onCreate(savedInstanceState)
        initMap()
    }

    private fun initMap() {
        val amap = mapView.map
        val giflist = ArrayList<BitmapDescriptor>()
        giflist.add(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
        giflist.add(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
        giflist.add(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW))
        val markerOption1 = MarkerOptions().anchor(0.5f, 0.5f).position(LatLng(lat, lng)).title(
                "所在地").snippet("所在地:" + location).icons(giflist).draggable(true).period(10)
        val markerOptionlst = ArrayList<MarkerOptions>()
        markerOptionlst.add(markerOption1)
        amap.addMarkers(markerOptionlst, true)

        amap.setOnInfoWindowClickListener { marker ->
            when (marker.isInfoWindowShown) {
                true -> marker.hideInfoWindow()
                else -> marker.showInfoWindow()
            }
        }
    }

    override fun initListener() {

    }

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        mapView.onSaveInstanceState(outState)
    }

    override fun onResume() {
        super.onResume()
        mapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        mapView.onPause()
    }


    override fun onDestroy() {
        super.onDestroy()
        mapView.onDestroy()
    }
}